package com.tool.excel.excelTest;

import com.alibaba.fastjson.JSONArray;
import com.tool.excel.excelTest.entity.Goods;
import com.tool.excel.excelTest.entity.Student;
import com.tool.excel.excelTest.entity.Teacher;
import com.tool.excel.utils.ExcelUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 酒徒
 * @Version 1.0
 * @data 2025/7/9 15:10
 */
@RestController
@RequestMapping("/excel")
public class ExcelController {

    @GetMapping("/readSignal")
    public void readSignal(@RequestBody MultipartFile file) throws Exception {
        file = getTestFile(0);
        List<Student> students = ExcelUtils.readMultipartFile(file, Student.class);
        System.out.println(students);
    }



    @GetMapping("/readManySheet")
    public void readManySheet(@RequestBody MultipartFile file) throws Exception {
        file = getTestFile(1);

        Map<String, JSONArray> map = ExcelUtils.readFileManySheet(file);

        map.forEach((key, value) -> {
            //key sheet名称, value sheet数据
            //value.toJavaList(Object.class) sheet转换成list, Object.class为转换的实体类对象， 注意使用@JsonImport注解
            if (key.equals("Student")) {
                List<Student> students = value.toJavaList(Student.class);
                System.out.println(students);
            } else if (key.equals("Teacher")) {
                List<Teacher> teachers = value.toJavaList(Teacher.class);
                System.out.println(teachers);
            } else if (key.equals("Goods")) {
                List<Goods> goods = value.toJavaList(Goods.class);
                System.out.println(goods);
            }
        });
    }

    @GetMapping("/export")
    public void exportSignal(HttpServletResponse response) throws Exception {
        MultipartFile file = getTestFile(0);
        List<Student> students = ExcelUtils.readMultipartFile(file, Student.class);
        ExcelUtils.export(response, "学生表", students, Student.class);
    }

    @GetMapping("/exportManySheet")
    public void exportManySheet(HttpServletResponse response) throws Exception {
        MultipartFile file = getTestFile(1);
        Map<String, JSONArray> map = ExcelUtils.readFileManySheet(file);
        Map<String, List<List<Object>>> sheetMap = new HashMap<>();
        map.forEach((key, value) -> {
            if (key.equals("Student")) {
                List<Student> students = value.toJavaList(Student.class);
                List<List<Object>> converted = new ArrayList<>();
                List<Object> headList = new ArrayList<>();
                headList.add("学号");
                headList.add("姓名");
                headList.add("科目");
                headList.add("分数");
                converted.add(headList);
                for (Student student : students) {
                    List<Object> info = new ArrayList<>();
                    info.add(student.getId());
                    info.add(student.getName());
                    info.add(student.getSubject());
                    info.add(student.getScore());
                    converted.add(info);
                }
                sheetMap.put("学生表", converted);
            } else if (key.equals("Teacher")) {
                List<Teacher> teachers = value.toJavaList(Teacher.class);
                List<List<Object>> converted = new ArrayList<>();
                List<Object> headList = new ArrayList<>();
                headList.add("城市");
                headList.add("特产");
                headList.add("评分");
                converted.add(headList);
                for (Teacher teacher : teachers) {
                    List<Object> info = new ArrayList<>();
                    info.add(teacher.getCity());
                    info.add(teacher.getSpeciality());
                    info.add(teacher.getScore());
                    converted.add(info);
                }
                sheetMap.put("教师表", converted);
            } else if (key.equals("Goods")) {
                List<Goods> goods = value.toJavaList(Goods.class);
                List<List<Object>> converted = new ArrayList<>();
                List<Object> headList = new ArrayList<>();
                headList.add("商品名称");
                headList.add("价格");
                headList.add("数量");
                converted.add(headList);
                for (Goods good : goods) {
                    List<Object> info = new ArrayList<>();
                    info.add(good.getName());
                    info.add(good.getPrice());
                    info.add(good.getCount());
                    converted.add(info);
                }
                sheetMap.put("商品表", converted);
            }
        });
        ExcelUtils.exportManySheet(response, "测试表", sheetMap);
    }


    /**
     * 本地excel测试
     * num 0 单sheet 1 多sheet
     */
    private static MultipartFile getTestFile(Integer num) throws IOException {

        ClassPathResource resource = new ClassPathResource("测试文档1.xlsx"); // 单sheet
        if (num == 1) {
            resource = new ClassPathResource("测试文档2.xlsx"); // 单sheet
        }
        MultipartFile file = new MockMultipartFile(
                "测试文档.xlsx",
                resource.getFilename(),
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                resource.getInputStream()
        );
        return file;
    }
}
